package com.igoal.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "user_friend_request")
@Searchable
public class UserFriend extends BaseObject implements Serializable, Audit {
	private static final long serialVersionUID = -1120296127434991111L;

	private Long id;
	private User from;
	private User to;
	private boolean accepted;
	private Date acceptOn;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o instanceof UserFriend) {
			UserFriend friend = (UserFriend) o;
			if (from == null && friend.from == null && to == null
					&& friend.to == null) {
				return true;
			}

			if (from != null && friend.from != null && from.equals(friend.from)
					&& to != null && friend.to != null && to.equals(friend.to)) {
				return true;
			}

			if (from != null && friend.from != null && from.equals(friend.from)
					&& to == null && friend.to == null) {
				return true;
			}

			if (from == null && friend.from == null && to != null
					&& friend.to != null && to.equals(friend.to)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return (from == null ? 13 : from.hashCode())
				+ (to == null ? 19 : to.hashCode());
	}

	@Override
	public String toString() {
		return from.getUsername() + " is friend of " + to.getUsername();
	}

	@ManyToOne
	@JoinColumn(name = "user_from", nullable = false)
	public User getFrom() {
		return from;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setFrom(User from) {
		this.from = from;
	}

	@ManyToOne
	@JoinColumn(name = "user_to", nullable = false)
	public User getTo() {
		return to;
	}

	public void setTo(User to) {
		this.to = to;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "status", nullable = false)
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	@Column(name = "accepted", nullable = false)
	@Type(type = "yes_no")
	public boolean isAccepted() {
		return accepted;
	}

	public void setAccepted(boolean accepted) {
		this.accepted = accepted;
	}

	@Column(name = "accept_on", nullable = true)
	public Date getAcceptOn() {
		return acceptOn;
	}

	public void setAcceptOn(Date acceptOn) {
		this.acceptOn = acceptOn;
	}

}
